package com.seata.logic.visualize.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seata.logic.visualize.dao.TblOrderMapper;
import com.seata.logic.visualize.dto.DecreaseStorageDto;
import com.seata.logic.visualize.dto.OrderCreateDto;
import com.seata.logic.visualize.entity.TblOrder;
import com.seata.logic.visualize.feign.StorageFeignService;
import com.seata.logic.visualize.service.TblOrderService;
import com.seata.logic.visualize.tcc.OrderTccAction;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author 蚂蚁会花呗
 * @date 2021/9/9 15:20
 */
@Service
public class TblOrderServiceImpl extends ServiceImpl<TblOrderMapper, TblOrder> implements TblOrderService {

    static Log log = LogFactory.getLog(TblOrderServiceImpl.class);

    @Autowired
    private OrderTccAction orderTccAction;

    @Autowired
    private StorageFeignService storageFeignService;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void create(OrderCreateDto createDto) {
        log.info("---------->开始交易");
        createDto.setId(IdWorker.get32UUID());
        boolean result = orderTccAction.createOrder(null,createDto);
//        if (result){
//            throw new RuntimeException();
//        }

        //扣减库存
        log.info("扣减库存开始");
        storageFeignService.decreaseStorage(new DecreaseStorageDto(createDto.getProductId(),createDto.getCount()));
        log.info("扣减库存结束");

        log.info("交易结束！");
    }

    public static void main(String[] args) {
        System.out.println(IdWorker.get32UUID());
    }
}
